﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HA.MT3DMS.FileLayer
{
    public abstract class Package
    {
        protected FileType _fileType;

        #region 辅助参数

        //计数器
        protected int counter;
        //辅助字符串
        protected string helpString = "";

        #endregion





        /// <summary>
        /// 返回文件类型字符串
        /// </summary>
        public string FileTypeString
        {
            get
            {


                return _fileType.ToString();

            }

        }

        uint _nunit;


        /// <summary>
        /// 文件单元，不可以是99
        /// </summary>
        public uint Nunit
        {
            get { return _nunit; }
            set { _nunit = value; }
        }


        string _fname;

        public string Fname
        {
            get { return _fname; }
            set { _fname = value; }
        }


        public abstract void WriteFile(string path);

        #region 辅助方法

        /// <summary>
        /// MT3D 字符串 int [,]
        /// </summary>
        /// <param name="array"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        protected bool GetArrayString(int[,] array, ref string str)
        {
            str = string.Format("{0,10}", 103) + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,20}", "(10G11.4)") + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + "\r\n"; 
           
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                for (int i = 0; i <= array.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= array.GetUpperBound(1); j++)
                    {
                        str += "  " + array[i, j];//string.Format("{0,10}", GetNumberLengthFormat(array[i, j], 10)); //
                    }
                    str += "\r\n";
                }
            }
            return true;
        }
        /// <summary>
        /// MT3D字符串double [,] 
        /// </summary>
        /// <param name="array"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        protected bool GetArrayString(double[,] array, ref string str)
        {
            str = string.Format("{0,10}", 103) + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,20}", "(10G11.4)") + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + "\r\n"; ;
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                for (int i = 0; i <= array.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= array.GetUpperBound(1); j++)
                    {
                        str += "  " + array[i, j];
                    }
                    str += "\r\n";
                }
            }
            return true;
        }
        /// <summary>
        /// MT3D List "double"
        /// </summary>
        /// <param name="array"></param>
        /// <param name="str"></param>
        /// <returns></returns>
        protected bool GetArrayString(List<double> array, ref string str)
        {
            str = string.Format("{0,10}", 103) + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + string.Format("{0,20}", "(10G11.4)") + string.Format("{0,10}", GetNumberLengthFormat(0, 10)) + "\r\n"; ;
            if (array.Count <= 0)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < array.Count; i++)
                {
                    str += "  " + array[i];
                }
                str += "\r\n";
            }
            return true;
        }
        ///// <summary>
        ///// mt3D
        ///// </summary>
        ///// <param name="array"></param>
        ///// <param name="str"></param>
        ///// <returns></returns>
        //protected bool GetArrayString(List <double > array, ref string str)
        //{
        //    str = "";
        //    if (array.Count  <= 0)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        for (int i = 0; i < array .Count ; i++)
        //        {
        //            str += array[i].ToString("0.00000E+00   ");
        //        }
        //        str += "\r\n";
        //    }
        //    return true;
        //}


        protected bool IsConstant(double[,] array)
        {
            double compare;
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                compare = array[0, 0];
                for (int i = 0; i <= array.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= array.GetUpperBound(1); j++)
                    {
                        if (array[i, j] != compare)
                        {
                            return false;
                        }
                    }
                }
            }

            return true;
        }
        protected bool IsConstant(int[] array)
        {
            int compare;
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                compare = array[0];
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] != compare)
                    {
                        return false;
                    }
                }
            }
            return true;
        }
        protected bool IsConstant(double[] array)
        {
            double compare;
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                compare = array[0];
                for (int i = 0; i < array.Length; i++)
                {
                    if (array[i] != compare)
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        protected bool IsConstant(List<double> array)
        {
            double compare;
            if (array.Count <= 0)
            {
                return false;
            }
            else
            {
                compare = array[0];
                for (int i = 0; i < array.Count; i++)
                {
                    if (array[i] != compare)
                    {
                        return false;
                    }
                }
            }
            return true;
        }


        protected bool GetArrayString(int[] array, ref string str)
        {
            str = "";
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                for (int i = 0; i < array.Length; i++)
                {
                    str += string.Format("{0,10}", GetNumberLengthFormat(array[i], 10));
                }
                str += "\r\n";
            }
            return true;
        }


        protected bool IsConstant(int[,] array)
        {
            int compare;
            if (array.Length <= 0)
            {
                return false;
            }
            else
            {
                compare = array[0, 0];
                for (int i = 0; i <= array.GetUpperBound(0); i++)
                {
                    for (int j = 0; j <= array.GetUpperBound(1); j++)
                    {
                        if (array[i, j] != compare)
                        {
                            return false;
                        }
                    }
                }

            }
            return true;
        }

        //protected bool IsConstant(double[] array)
        //{
        //    double compare;
        //    if (array.Length <= 0)
        //    {
        //        return false;
        //    }
        //    else
        //    {
        //        compare = array[0];
        //        for (int i = 0; i < array.Length; i++)
        //        {
        //            if (array[i] != compare)
        //            {
        //                return false;
        //            }
        //        }
        //    }
        //    return true;
        //}

        /// <summary>
        /// 截取指定长度的数字
        /// </summary>
        /// <param name="number"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public double GetNumberLengthFormat(double number, int count)
        {
            if (number.ToString().Length >= count)
            {
                return Convert.ToDouble(number.ToString().Substring(0, count - 1));
            }
            else
            {
                return number;
            }

        }
        /// <summary>
        /// 截取指定长度的数字
        /// </summary>
        /// <param name="number"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public int GetNumberLengthFormat(int number, int count)
        {
            if (number.ToString().Length >= count)
            {
                return Convert.ToInt32(number.ToString().Substring(0, count - 1));
            }
            else
            {
                return number;
            }

        }
        /// <summary>
        /// 截取指定长度的字符
        /// </summary>
        /// <param name="str"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string GetNumberLengthFormat(string str, int count)
        {
            if (str.ToString().Length >= count)
            {
                return str.ToString().Substring(0, count - 1);
            }
            else
            {
                return str;
            }

        }
        #endregion
    }
}
